package c.a.a.c0;

import android.content.Context;
import c.a.a.f;
import c.a.a.g;
import c.a.a.r;
import c.b.a.a.a;
import com.huawei.agconnect.exception.AGCServerException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.ZipInputStream;

/* compiled from: NetworkFetcher */
public class c {
    public final Context a;
    public final String b;
    public final b c;

    public c(Context context, String str, String str2) {
        context = context.getApplicationContext();
        this.a = context;
        this.b = str;
        if (str2 == null) {
            this.c = null;
        } else {
            this.c = new b(context);
        }
    }

    public final r<f> a() throws IOException {
        StringBuilder g = a.g("Fetching ");
        g.append(this.b);
        c.a.a.e0.c.a(g.toString());
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(this.b).openConnection();
        httpURLConnection.setRequestMethod("GET");
        r<f> rVar;
        try {
            httpURLConnection.connect();
            if (httpURLConnection.getErrorStream() == null) {
                int responseCode = httpURLConnection.getResponseCode();
                rVar = AGCServerException.OK;
                if (responseCode == AGCServerException.OK) {
                    r c = c(httpURLConnection);
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append("Completed fetch from network. Success: ");
                    stringBuilder.append(c.a != null);
                    c.a.a.e0.c.a(stringBuilder.toString());
                    httpURLConnection.disconnect();
                    return c;
                }
            }
            String b = b(httpURLConnection);
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.append("Unable to fetch ");
            stringBuilder2.append(this.b);
            stringBuilder2.append(". Failed with ");
            stringBuilder2.append(httpURLConnection.getResponseCode());
            stringBuilder2.append("\n");
            stringBuilder2.append(b);
            rVar = new r(new IllegalArgumentException(stringBuilder2.toString()));
            return rVar;
        } catch (Exception e) {
            rVar = new r(e);
            return rVar;
        } finally {
            httpURLConnection.disconnect();
        }
    }

    public final String b(HttpURLConnection httpURLConnection) throws IOException {
        httpURLConnection.getResponseCode();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream()));
        StringBuilder stringBuilder = new StringBuilder();
        while (true) {
            try {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    stringBuilder.append(readLine);
                    stringBuilder.append(10);
                } else {
                    try {
                        break;
                    } catch (Exception unused) {
                    }
                }
            } catch (Exception e) {
                throw e;
            } catch (Throwable th) {
                try {
                    bufferedReader.close();
                } catch (Exception unused2) {
                }
            }
        }
        bufferedReader.close();
        return stringBuilder.toString();
    }

    public final r<f> c(HttpURLConnection httpURLConnection) throws IOException {
        a aVar;
        r<f> g;
        String contentType = httpURLConnection.getContentType();
        if (contentType == null) {
            contentType = "application/json";
        }
        b bVar;
        if (contentType.contains("application/zip")) {
            c.a.a.e0.c.a("Handling zip response.");
            aVar = a.ZIP;
            bVar = this.c;
            if (bVar == null) {
                g = g.g(new ZipInputStream(httpURLConnection.getInputStream()), null);
            } else {
                g = g.g(new ZipInputStream(new FileInputStream(bVar.c(this.b, httpURLConnection.getInputStream(), aVar))), this.b);
            }
        } else {
            c.a.a.e0.c.a("Received json response.");
            aVar = a.JSON;
            bVar = this.c;
            if (bVar == null) {
                g = g.d(httpURLConnection.getInputStream(), null);
            } else {
                g = g.d(new FileInputStream(new File(bVar.c(this.b, httpURLConnection.getInputStream(), aVar).getAbsolutePath())), this.b);
            }
        }
        b bVar2 = this.c;
        if (!(bVar2 == null || g.a == null)) {
            File file = new File(bVar2.b(), b.a(this.b, aVar, true));
            File file2 = new File(file.getAbsolutePath().replace(".temp", ""));
            boolean renameTo = file.renameTo(file2);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Copying temp file to real file (");
            stringBuilder.append(file2);
            stringBuilder.append(")");
            c.a.a.e0.c.a(stringBuilder.toString());
            if (!renameTo) {
                StringBuilder g2 = a.g("Unable to rename cache file ");
                g2.append(file.getAbsolutePath());
                g2.append(" to ");
                g2.append(file2.getAbsolutePath());
                g2.append(".");
                c.a.a.e0.c.b(g2.toString());
            }
        }
        return g;
    }
}
